home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC 1.37.1r15 Full / Sources / tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-09  |  59.7 KB  |  2,279 lines  |  [TEXT/MPS ]

  1. /* Language-indepednent node constructors for parse phase of GNU compiler.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.    Copyright (C) 1989, 1990 Apple Computer, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* This file contains the low level primitives for operating on tree nodes,
  23.    including allocation, list operations, interning of identifiers,
  24.    construction of data type nodes and statement nodes,
  25.    and construction of type conversion nodes.  It also contains
  26.    tables index by tree code that describe how to take apart
  27.    nodes of that code.
  28.  
  29.    It is intended to be language-independent, but occasionally
  30.    calls language-dependent routines defined (for C) in typecheck.c.
  31.  
  32.    The low-level allocation routines oballoc and permalloc
  33.    are used also for allocating many other kinds of objects
  34.    by all passes of the compiler.  */
  35.  
  36. #include "config.h"
  37. #include <stdio.h>
  38. #include "tree.h"
  39. #include "obstack.h"
  40. #include "gvarargs.h"
  41. #include "flags.h"
  42.  
  43. #define obstack_chunk_alloc xmalloc
  44. #define obstack_chunk_free free
  45.  
  46. extern int xmalloc ();
  47. extern void free ();
  48.  
  49. /* Tree nodes of permanent duration are allocated in this obstack.
  50.    They are the identifier nodes, and everything outside of
  51.    the bodies and parameters of function definitions.  */
  52.  
  53. struct obstack permanent_obstack;
  54.  
  55. /* The initial RTL, and all ..._TYPE nodes, in a function
  56.    are allocated in this obstack.  Usually they are freed at the
  57.    end of the function, but if the function is inline they are saved.  */
  58.  
  59. struct obstack maybepermanent_obstack;
  60.  
  61. /* The contents of the current function definition are allocated
  62.    in this obstack, and all are freed at the end of the function.  */
  63.  
  64. struct obstack temporary_obstack;
  65.  
  66. /* The tree nodes of an expression are allocated
  67.    in this obstack, and all are freed at the end of the expression.  */
  68.  
  69. struct obstack momentary_obstack;
  70.  
  71. /* This points at either permanent_obstack or maybepermanent_obstack.  */
  72.  
  73. struct obstack *saveable_obstack;
  74.  
  75. /* This is same as saveable_obstack during parse and expansion phase;
  76.    it points to temporary_obstack during optimization.
  77.    This is the obstack to be used for creating rtl objects.  */
  78.  
  79. struct obstack *rtl_obstack;
  80.  
  81. /* This points at either permanent_obstack or temporary_obstack.  */
  82.  
  83. struct obstack *current_obstack;
  84.  
  85. /* This points at either permanent_obstack or temporary_obstack
  86.    or momentary_obstack.  */
  87.  
  88. struct obstack *expression_obstack;
  89.  
  90. /* Addresses of first objects in some obstacks.
  91.    This is for freeing their entire contents.  */
  92. char *maybepermanent_firstobj;
  93. char *temporary_firstobj;
  94. char *momentary_firstobj;
  95.  
  96. /* Nonzero means all ..._TYPE nodes should be allocated permanently.  */
  97.  
  98. int all_types_permanent;
  99.  
  100. /* Stack of places to restore the momentary obstack back to.  */
  101.    
  102. struct momentary_level
  103. {
  104.   /* Pointer back to previous such level.  */
  105.   struct momentary_level *prev;
  106.   /* First object allocated within this level.  */
  107.   char *base;
  108.   /* Value of expression_obstack saved at entry to this level.  */
  109.   struct obstack *obstack;
  110. };
  111.  
  112. struct momentary_level *momentary_stack;
  113.  
  114. /* Table indexed by tree code giving a string containing a character
  115.    classifying the tree code.  Possibilities are
  116.    t, d, s, c, r and e.  See tree.def for details.  */
  117.  
  118. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
  119.  
  120. char *tree_code_type[] = {
  121. #include "tree.def"
  122. };
  123. #undef DEFTREECODE
  124.  
  125. /* Table indexed by tree code giving number of expression
  126.    operands beyond the fixed part of the node structure.
  127.    Not used for types or decls.  */
  128.  
  129. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
  130.  
  131. int tree_code_length[] = {
  132. #include "tree.def"
  133. };
  134. #undef DEFTREECODE
  135.  
  136. /* Counter for assigning unique ids to all tree nodes.  */
  137.  
  138. int tree_node_counter = 0;
  139.  
  140. /* Hash table for uniquizing IDENTIFIER_NODEs by name.  */
  141.  
  142. #define MAX_HASH_TABLE 1009
  143. #ifdef MPW
  144. /* Save some space by dynamically allocating the hash table. */
  145. static tree *hash_table;
  146. #else
  147. static tree hash_table[MAX_HASH_TABLE];    /* id hash buckets */
  148. #endif /* MPW */
  149.  
  150. /* 0 while creating built-in identifiers.  */
  151. static int do_identifier_warnings;
  152.  
  153. /* Init data for node creation, at the beginning of compilation.  */
  154.  
  155. void
  156. init_tree ()
  157. {
  158.   obstack_init (&permanent_obstack);
  159.  
  160.   obstack_init (&temporary_obstack);
  161.   temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
  162.   obstack_init (&momentary_obstack);
  163.   momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
  164.   obstack_init (&maybepermanent_obstack);
  165.   maybepermanent_firstobj
  166.     = (char *) obstack_alloc (&maybepermanent_obstack, 0);
  167.  
  168.   current_obstack = &permanent_obstack;
  169.   expression_obstack = &permanent_obstack;
  170.   rtl_obstack = saveable_obstack = &permanent_obstack;
  171.   tree_node_counter = 1;
  172. #ifdef MPW
  173.   /* Allocate the hash table. */
  174.   hash_table = (tree *) xmalloc((sizeof( tree)) * MAX_HASH_TABLE);
  175.   bzero (hash_table, (sizeof(tree)) * MAX_HASH_TABLE);
  176. #else
  177.   bzero (hash_table, sizeof hash_table);
  178. #endif /* MPW */
  179. }
  180.  
  181. /* Start allocating on the temporary (per function) obstack.
  182.    This is done in start_function before parsing the function body,
  183.    and before each initialization at top level, and to go back
  184.    to temporary allocation after doing end_temporary_allocation.  */
  185.  
  186. void
  187. temporary_allocation ()
  188. {
  189.   current_obstack = &temporary_obstack;
  190.   expression_obstack = &temporary_obstack;
  191.   rtl_obstack = saveable_obstack = &maybepermanent_obstack;
  192.   momentary_stack = 0;
  193. }
  194.  
  195. /* Start allocating on the permanent obstack but don't
  196.    free the temporary data.  After calling this, call
  197.    `permanent_allocation' to fully resume permanent allocation status.  */
  198.  
  199. void
  200. end_temporary_allocation ()
  201. {
  202.   current_obstack = &permanent_obstack;
  203.   expression_obstack = &permanent_obstack;
  204.   rtl_obstack = saveable_obstack = &permanent_obstack;
  205. }
  206.  
  207. /* Resume allocating on the temporary obstack, undoing
  208.    effects of `end_temporary_allocation'.  */
  209.  
  210. void
  211. resume_temporary_allocation ()
  212. {
  213.   current_obstack = &temporary_obstack;
  214.   expression_obstack = &temporary_obstack;
  215.   rtl_obstack = saveable_obstack = &maybepermanent_obstack;
  216. }
  217.  
  218. /* Nonzero if temporary allocation is currently in effect.
  219.    Zero if currently doing permanent allocation.  */
  220.  
  221. int
  222. allocation_temporary_p ()
  223. {
  224.   return current_obstack == &temporary_obstack;
  225. }
  226.  
  227. /* Go back to allocating on the permanent obstack
  228.    and free everything in the temporary obstack.
  229.    This is done in finish_function after fully compiling a function.  */
  230.  
  231. void
  232. permanent_allocation ()
  233. {
  234.   /* Free up previous temporary obstack data */
  235.   obstack_free (&temporary_obstack, temporary_firstobj);
  236.   obstack_free (&momentary_obstack, momentary_firstobj);
  237.   obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
  238.  
  239.   current_obstack = &permanent_obstack;
  240.   expression_obstack = &permanent_obstack;
  241.   rtl_obstack = saveable_obstack = &permanent_obstack;
  242. }
  243.  
  244. /* Save permanently everything on the maybepermanent_obstack.  */
  245.  
  246. void
  247. preserve_data ()
  248. {
  249.   maybepermanent_firstobj
  250.     = (char *) obstack_alloc (&maybepermanent_obstack, 0);
  251. }
  252.  
  253. /* Allocate SIZE bytes in the current obstack
  254.    and return a pointer to them.
  255.    In practice the current obstack is always the temporary one.  */
  256.  
  257. char *
  258. oballoc (size)
  259.      int size;
  260. {
  261.   return (char *) obstack_alloc (current_obstack, size);
  262. }
  263.  
  264. /* Free the object PTR in the current obstack
  265.    as well as everything allocated since PTR.
  266.    In practice the current obstack is always the temporary one.  */
  267.  
  268. void
  269. obfree (ptr)
  270.      char *ptr;
  271. {
  272. #ifdef MPW_C
  273.   /* This doesn't make any sense, but the MPW C RA is happier... */
  274.   int tmp;
  275.     
  276.   current_obstack->temp = ptr - (char *) current_obstack->chunk;
  277.   if (current_obstack->temp >= 0) {
  278.       tmp = current_obstack->chunk_limit - ((char *) current_obstack->chunk);
  279.       if (current_obstack->temp < tmp) {
  280.       current_obstack->object_base =
  281.         current_obstack->temp + (char *) current_obstack->chunk;
  282.       current_obstack->next_free = current_obstack->object_base;
  283.       return;
  284.       }
  285.   }
  286.   _obstack_free (current_obstack,
  287.          current_obstack->temp + (char *) current_obstack->chunk);
  288. #else /* what this is supposed to be */
  289.   obstack_free (current_obstack, ptr);
  290. #endif /* MPW_C */
  291. }
  292.  
  293. /* Allocate SIZE bytes in the permanent obstack
  294.    and return a pointer to them.  */
  295.  
  296. char *
  297. permalloc (size)
  298.      long size;
  299. {
  300.   return (char *) obstack_alloc (&permanent_obstack, size);
  301. }
  302.  
  303. /* Allocate SIZE bytes in the saveable obstack
  304.    and return a pointer to them.  */
  305.  
  306. char *
  307. savealloc (size)
  308.      int size;
  309. {
  310.   return (char *) obstack_alloc (saveable_obstack, size);
  311. }
  312.  
  313. /* Start a level of momentary allocation.
  314.    In C, each compound statement has its own level
  315.    and that level is freed at the end of each statement.
  316.    All expression nodes are allocated in the momentary allocation level.  */
  317.  
  318. void
  319. push_momentary ()
  320. {
  321.   struct momentary_level *tem
  322.     = (struct momentary_level *) obstack_alloc (&momentary_obstack,
  323.                         sizeof (struct momentary_level));
  324.   tem->prev = momentary_stack;
  325.   tem->base = (char *) obstack_base (&momentary_obstack);
  326.   tem->obstack = expression_obstack;
  327.   momentary_stack = tem;
  328.   expression_obstack = &momentary_obstack;
  329. }
  330.  
  331. /* Free all the storage in the current momentary-allocation level.
  332.    In C, this happens at the end of each statement.  */
  333.  
  334. void
  335. clear_momentary ()
  336. {
  337.   obstack_free (&momentary_obstack, momentary_stack->base);
  338. }
  339.  
  340. /* Discard a level of momentary allocation.
  341.    In C, this happens at the end of each compound statement.
  342.    Restore the status of expression node allocation
  343.    that was in effect before this level was created.  */
  344.  
  345. void
  346. pop_momentary ()
  347. {
  348.   struct momentary_level *tem = momentary_stack;
  349.   momentary_stack = tem->prev;
  350.   obstack_free (&momentary_obstack, tem);
  351.   expression_obstack = tem->obstack;
  352. }
  353.  
  354. /* Call when starting to parse a declaration:
  355.    make expressions in the declaration last the length of the function.
  356.    Returns an argument that should be passed to resume_momentary later.  */
  357.  
  358. int
  359. suspend_momentary ()
  360. {
  361.   register int tem = expression_obstack == &momentary_obstack;
  362.   expression_obstack = saveable_obstack;
  363.   return tem;
  364. }
  365.  
  366. /* Call when finished parsing a declaration:
  367.    restore the treatment of node-allocation that was
  368.    in effect before the suspension.
  369.    YES should be the value previously returned by suspend_momentary.  */
  370.  
  371. void
  372. resume_momentary (yes)
  373.      int yes;
  374. {
  375.   if (yes)
  376.     expression_obstack = &momentary_obstack;
  377. }
  378.  
  379. /* Return a newly allocated node of code CODE.
  380.    Initialize the node's unique id and its TREE_PERMANENT flag.
  381.    For decl and type nodes, some other fields are initialized.
  382.    The rest of the node is initialized to zero.
  383.  
  384.    Achoo!  I got a code in the node.  */
  385.  
  386. tree
  387. make_node (code)
  388.      enum tree_code code;
  389. {
  390.   register tree t;
  391.   register int type = *tree_code_type[(int) code];
  392.   register int length;
  393.   register struct obstack *obstack = current_obstack;
  394.   register int i;
  395.  
  396.   switch (type)
  397.     {
  398.     case 'd':  /* A decl node */
  399.       length = sizeof (struct tree_decl);
  400.       /* All decls in an inline function need to be saved.  */
  401.       if (obstack != &permanent_obstack)
  402.     obstack = saveable_obstack;
  403.       /* PARM_DECLs always go on saveable_obstack, not permanent,
  404.      even though we may make them before the function turns
  405.      on temporary allocation.  */
  406.       else if (code == PARM_DECL)
  407.     obstack = &maybepermanent_obstack;
  408.       break;
  409.  
  410.     case 't':  /* a type node */
  411.       length = sizeof (struct tree_type);
  412.       /* All data types are put where we can preserve them if nec.  */
  413.       if (obstack != &permanent_obstack)
  414.     obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
  415.       break;
  416.  
  417.     case 's':  /* a stmt node */
  418.       length = sizeof (struct tree_common)
  419.     + 2 * sizeof (int)
  420.       + tree_code_length[(int) code] * sizeof (char *);
  421.       /* All stmts are put where we can preserve them if nec.  */
  422.       if (obstack != &permanent_obstack)
  423.     obstack = saveable_obstack;
  424.       break;
  425.  
  426.     case 'r':  /* a reference */
  427.     case 'e':  /* an expression */
  428.       obstack = expression_obstack;
  429.       length = sizeof (struct tree_exp)
  430.     + (tree_code_length[(int) code] - 1) * sizeof (char *);
  431.       break;
  432.  
  433.     case 'c':  /* a constant */
  434.       obstack = expression_obstack;
  435.       /* We can't use tree_code_length for this, since the number of words
  436.      is machine-dependent due to varying alignment of `double'.  */
  437.       if (code == REAL_CST)
  438.     {
  439.       length = sizeof (struct tree_real_cst);
  440.       break;
  441.     }
  442.  
  443.     case 'x':  /* something random, like an identifier.  */
  444.       length = sizeof (struct tree_common)
  445.     + tree_code_length[(int) code] * sizeof (char *);
  446.       /* Identifier nodes are always permanent since they are
  447.      unique in a compiler run.  */
  448.       if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
  449.     }
  450.  
  451.   t = (tree) obstack_alloc (obstack, length);
  452.  
  453.   TREE_UID (t) = tree_node_counter++;
  454.   TREE_TYPE (t) = 0;
  455.   TREE_CHAIN (t) = 0;
  456.   for (i = (length / sizeof (int)) - 1;
  457.        i >= sizeof (struct tree_common) / sizeof (int) - 1;
  458.        i--)
  459.     ((int *) t)[i] = 0;
  460.  
  461.   TREE_SET_CODE (t, code);
  462.   if (obstack == &permanent_obstack)
  463.     TREE_PERMANENT (t) = 1;
  464.  
  465.   if (type == 'd')
  466.     {
  467.       extern int lineno;
  468.  
  469.       DECL_ALIGN (t) = 1;
  470.       DECL_SIZE_UNIT (t) = 1;
  471.       DECL_VOFFSET_UNIT (t) = 1;
  472.       DECL_SOURCE_LINE (t) = lineno;
  473.       DECL_SOURCE_FILE (t) = input_filename;
  474.       DECL_PARAM(t) = NULL;
  475.     }
  476.  
  477.   if (type == 't')
  478.     {
  479.       TYPE_ALIGN (t) = 1;
  480.       TYPE_SIZE_UNIT (t) = 1;
  481.       TYPE_MAIN_VARIANT (t) = t;
  482.     }
  483.  
  484.   if (type == 'c')
  485.     {
  486.       TREE_LITERAL (t) = 1;
  487.     }
  488.  
  489.   return t;
  490. }
  491.  
  492. /* Return a new node with the same contents as NODE
  493.    except that its TREE_CHAIN is zero and it has a fresh uid.  */
  494.  
  495. tree
  496. copy_node (node)
  497.      tree node;
  498. {
  499.   register tree t;
  500.   register enum tree_code code = TREE_CODE (node);
  501.   register int length;
  502.   register int i;
  503.  
  504.   switch (*tree_code_type[(int) code])
  505.     {
  506.     case 'd':  /* A decl node */
  507.       length = sizeof (struct tree_decl);
  508.       break;
  509.  
  510.     case 't':  /* a type node */
  511.       length = sizeof (struct tree_type);
  512.       break;
  513.  
  514.     case 's':
  515.       length = sizeof (struct tree_common)
  516.     + 2 * sizeof (int)
  517.       + tree_code_length[(int) code] * sizeof (char *);
  518.       break;
  519.  
  520.     case 'r':  /* a reference */
  521.     case 'e':  /* a expression */
  522.       length = sizeof (struct tree_exp)
  523.     + (tree_code_length[(int) code] - 1) * sizeof (char *);
  524.       break;
  525.  
  526.     case 'c':  /* a constant */
  527.       /* We can't use tree_code_length for this, since the number of words
  528.      is machine-dependent due to varying alignment of `double'.  */
  529.       if (code == REAL_CST)
  530.     {
  531.       length = sizeof (struct tree_real_cst);
  532.       break;
  533.     }
  534.  
  535.     case 'x':  /* something random, like an identifier.  */
  536.       length = sizeof (struct tree_common)
  537.     + tree_code_length[(int) code] * sizeof (char *);
  538.     }
  539.  
  540.   t = (tree) obstack_alloc (current_obstack, length);
  541.  
  542.   for (i = ((length + sizeof (int) - 1) / sizeof (int)) - 1;
  543.        i >= 0;
  544.        i--)
  545.     ((int *) t)[i] = ((int *) node)[i];
  546.  
  547.   TREE_UID (t) = tree_node_counter++;
  548.   TREE_CHAIN (t) = 0;
  549.  
  550.   TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
  551.  
  552.   return t;
  553. }
  554.  
  555. /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
  556.    For example, this can copy a list made of TREE_LIST nodes.  */
  557.  
  558. tree
  559. copy_list (list)
  560.      tree list;
  561. {
  562.   tree head;
  563.   register tree prev, next;
  564.  
  565.   if (list == 0)
  566.     return 0;
  567.  
  568.   head = prev = copy_node (list);
  569.   next = TREE_CHAIN (list);
  570.   while (next)
  571.     {
  572.       TREE_CHAIN (prev) = copy_node (next);
  573.       prev = TREE_CHAIN (prev);
  574.       next = TREE_CHAIN (next);
  575.     }
  576.   return head;
  577. }
  578.  
  579. #define HASHBITS 30
  580.  
  581. /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
  582.    If an identifier with that name has previously been referred to,
  583.    the same node is returned this time.  */
  584.  
  585. tree
  586. get_identifier (text)
  587.      register char *text;
  588. {
  589.   register int hi;
  590.   register int i;
  591.   register tree idp;
  592.   register int len, hash_len;
  593.  
  594.   /* Compute length of text in len.  */
  595.   for (len = 0; text[len]; len++);
  596.  
  597.   /* Decide how much of that length to hash on */
  598.   hash_len = len;
  599.   if (warn_id_clash && len > id_clash_len)
  600.     hash_len = id_clash_len;
  601.  
  602.   /* Compute hash code */
  603.   hi = hash_len;
  604.   for (i = 0; i < hash_len; i++)
  605.     hi = ((hi * 613) + (unsigned)(text[i]));
  606.  
  607.   hi &= (1 << HASHBITS) - 1;
  608.   hi %= MAX_HASH_TABLE;
  609.   
  610.   /* Search table for identifier */
  611.   for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
  612.     if (IDENTIFIER_LENGTH (idp) == len
  613.     && !strcmp (IDENTIFIER_POINTER (idp), text))
  614.       return idp;        /* <-- return if found */
  615.   
  616.   /* Not found; optionally warn about a similar identifier */
  617.   if (warn_id_clash && do_identifier_warnings && len > id_clash_len)
  618.     for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
  619.       if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
  620.     {
  621.       warning ("`%s' and `%s' identical in first n characters",
  622.            IDENTIFIER_POINTER (idp), text);
  623.       break;
  624.     }
  625.  
  626.   /* Not found, create one, add to chain */
  627.   idp = make_node (IDENTIFIER_NODE);
  628.   IDENTIFIER_LENGTH (idp) = len;
  629.  
  630.   IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
  631.  
  632.   TREE_CHAIN (idp) = hash_table[hi];
  633.   hash_table[hi] = idp;
  634.   return idp;            /* <-- return if created */
  635. }
  636.  
  637. /* Enable warnings on similar identifiers (if requested).
  638.    Done after the built-in identifiers are created.  */
  639.  
  640. void
  641. start_identifier_warnings ()
  642. {
  643.   do_identifier_warnings = 1;
  644. }
  645.  
  646. /* Record the size of an identifier node for the language in use.
  647.    This is called by the language-specific files.  */
  648.  
  649. void
  650. set_identifier_size (size)
  651.      int size;
  652. {
  653.   tree_code_length[(int) IDENTIFIER_NODE] = size;
  654. }
  655.  
  656. /* Return a newly constructed INTEGER_CST node whose constant value
  657.    is specified by the two ints LOW and HI.
  658.    The TREE_TYPE is set to `int'.  */
  659.  
  660. tree
  661. build_int_2 (low, hi)
  662.      int low, hi;
  663. {
  664.   register tree t = make_node (INTEGER_CST);
  665.   TREE_INT_CST_LOW (t) = low;
  666.   TREE_INT_CST_HIGH (t) = hi;
  667.   TREE_TYPE (t) = integer_type_node;
  668.   return t;
  669. }
  670.  
  671. /* Return a new REAL_CST node whose type is TYPE and value is D.  */
  672.  
  673. tree
  674. build_real (type, d)
  675.      tree type;
  676.      REAL_VALUE_TYPE d;
  677. {
  678.   tree v;
  679.  
  680.   /* Check for valid float value for this type on this target machine;
  681.      if not, can print error message and store a valid value in D.  */
  682. #ifdef CHECK_FLOAT_VALUE
  683.   CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
  684. #endif
  685.  
  686.   v = make_node (REAL_CST);
  687.   TREE_TYPE (v) = type;
  688.   TREE_REAL_CST (v) = d;
  689.   return v;
  690. }
  691.  
  692. /* Return a new REAL_CST node whose type is TYPE
  693.    and whose value is the integer value of the INTEGER_CST node I.  */
  694.  
  695. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  696.  
  697. REAL_VALUE_TYPE
  698. real_value_from_int_cst (i)
  699.      tree i;
  700. {
  701.   REAL_VALUE_TYPE d;
  702. #ifdef REAL_ARITHMETIC
  703.   REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
  704. #else /* not REAL_ARITHMETIC */
  705.   if (TREE_INT_CST_HIGH (i) < 0)
  706.     {
  707.       d = (double) (~ TREE_INT_CST_HIGH (i));
  708.       d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
  709.         * (double) (1 << (HOST_BITS_PER_INT / 2)));
  710.       d += (double) (unsigned) (~ TREE_INT_CST_LOW (i));
  711.       d = (- d - 1.0);
  712.     }
  713.   else
  714.     {
  715.       d = (double) TREE_INT_CST_HIGH (i);
  716.       d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
  717.         * (double) (1 << (HOST_BITS_PER_INT / 2)));
  718.       d += (double) (unsigned) TREE_INT_CST_LOW (i);
  719.     }
  720. #endif /* not REAL_ARITHMETIC */
  721.   return d;
  722. }
  723.  
  724. /* This function can't be implemented if we can't do arithmetic
  725.    on the float representation.  */
  726.  
  727. tree
  728. build_real_from_int_cst (type, i)
  729.      tree type;
  730.      tree i;
  731. {
  732.   tree v;
  733.   REAL_VALUE_TYPE d;
  734.  
  735.   v = make_node (REAL_CST);
  736.   TREE_TYPE (v) = type;
  737.  
  738.   d = real_value_from_int_cst (i);
  739.   /* Check for valid float value for this type on this target machine;
  740.      if not, can print error message and store a valid value in D.  */
  741. #ifdef CHECK_FLOAT_VALUE
  742.   CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
  743. #endif
  744.  
  745.   TREE_REAL_CST (v) = d;
  746.   return v;
  747. }
  748.  
  749. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  750.  
  751. /* Return a newly constructed STRING_CST node whose value is
  752.    the LEN characters at STR.
  753.    The TREE_TYPE is not initialized.  */
  754.  
  755. tree
  756. build_string (len, str)
  757.      int len;
  758.      char *str;
  759. {
  760.   register tree s = make_node (STRING_CST);
  761.   TREE_STRING_LENGTH (s) = len;
  762.   TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
  763.   return s;
  764. }
  765.  
  766. /* Return a newly constructed COMPLEX_CST node whose value is
  767.    specified by the real and imaginary parts REAL and IMAG.
  768.    Both REAL and IMAG should be constant nodes.
  769.    The TREE_TYPE is not initialized.  */
  770.  
  771. tree
  772. build_complex (real, imag)
  773.      tree real, imag;
  774. {
  775.   register tree t = make_node (COMPLEX_CST);
  776.   TREE_REALPART (t) = real;
  777.   TREE_IMAGPART (t) = imag;
  778.   return t;
  779. }
  780.  
  781. /* Return 1 if EXPR is the integer constant zero.  */
  782.  
  783. int
  784. integer_zerop (expr)
  785.      tree expr;
  786. {
  787.   return (TREE_CODE (expr) == INTEGER_CST
  788.       && TREE_INT_CST_LOW (expr) == 0
  789.       && TREE_INT_CST_HIGH (expr) == 0);
  790. }
  791.  
  792. /* Return 1 if EXPR is the integer constant one.  */
  793.  
  794. int
  795. integer_onep (expr)
  796.      tree expr;
  797. {
  798.   return (TREE_CODE (expr) == INTEGER_CST
  799.       && TREE_INT_CST_LOW (expr) == 1
  800.       && TREE_INT_CST_HIGH (expr) == 0);
  801. }
  802.  
  803. /* Return 1 if EXPR is an integer containing all 1's
  804.    in as much precision as it contains.  */
  805.  
  806. int
  807. integer_all_onesp (expr)
  808.      tree expr;
  809. {
  810.   register int prec;
  811.   register int uns;
  812.  
  813.   if (TREE_CODE (expr) != INTEGER_CST)
  814.     return 0;
  815.  
  816.   uns = TREE_UNSIGNED (TREE_TYPE (expr));
  817.   if (!uns)
  818.     return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
  819.  
  820.   prec = TYPE_PRECISION (TREE_TYPE (expr));
  821.   if (prec >= HOST_BITS_PER_INT)
  822.     return TREE_INT_CST_LOW (expr) == -1
  823.       && TREE_INT_CST_HIGH (expr) == (1 << (prec - HOST_BITS_PER_INT)) - 1;
  824.   else
  825.     return TREE_INT_CST_LOW (expr) == (1 << prec) - 1;
  826. }
  827.  
  828. /* Return the length of a chain of nodes chained through TREE_CHAIN.
  829.    We expect a null pointer to mark the end of the chain.
  830.    This is the Lisp primitive `length'.  */
  831.  
  832. int
  833. list_length (t)
  834.      tree t;
  835. {
  836.   register tree tail;
  837.   register int len = 0;
  838.  
  839.   for (tail = t; tail; tail = TREE_CHAIN (tail))
  840.     len++;
  841.  
  842.   return len;
  843. }
  844.  
  845. /* Concatenate two chains of nodes (chained through TREE_CHAIN)
  846.    by modifying the last node in chain 1 to point to chain 2.
  847.    This is the Lisp primitive `nconc'.  */
  848.  
  849. tree
  850. chainon (op1, op2)
  851.      tree op1, op2;
  852. {
  853.   tree t;
  854.  
  855.   if (op1)
  856.     {
  857.       for (t = op1; TREE_CHAIN (t); t = TREE_CHAIN (t))
  858.     if (t == op2) abort ();    /* Circularity being created */
  859.       TREE_CHAIN (t) = op2;
  860.       return op1;
  861.     }
  862.   else return op2;
  863. }
  864.  
  865. /* Return a newly created TREE_LIST node whose
  866.    purpose and value fields are PARM and VALUE.  */
  867.  
  868. tree
  869. build_tree_list (parm, value)
  870.      tree parm, value;
  871. {
  872.   register tree t = make_node (TREE_LIST);
  873.   TREE_PURPOSE (t) = parm;
  874.   TREE_VALUE (t) = value;
  875.   return t;
  876. }
  877.  
  878. /* Return a newly created TREE_LIST node whose
  879.    purpose and value fields are PARM and VALUE
  880.    and whose TREE_CHAIN is CHAIN.  */
  881.  
  882. tree
  883. tree_cons (purpose, value, chain)
  884.      tree purpose, value, chain;
  885. {
  886.   register tree node = make_node (TREE_LIST);
  887.   TREE_CHAIN (node) = chain;
  888.   TREE_PURPOSE (node) = purpose;
  889.   TREE_VALUE (node) = value;
  890.   return node;
  891. }
  892.  
  893. /* Same as `tree_cons' but make a permanent object.  */
  894.  
  895. tree
  896. perm_tree_cons (purpose, value, chain)
  897.      tree purpose, value, chain;
  898. {
  899.   register tree node;
  900.   register struct obstack *ambient_obstack = current_obstack;
  901.   current_obstack = &permanent_obstack;
  902.  
  903.   node = make_node (TREE_LIST);
  904.   TREE_CHAIN (node) = chain;
  905.   TREE_PURPOSE (node) = purpose;
  906.   TREE_VALUE (node) = value;
  907.  
  908.   current_obstack = ambient_obstack;
  909.   return node;
  910. }
  911.  
  912. /* Same as `tree_cons', but make this node temporary, regardless.  */
  913.  
  914. tree
  915. temp_tree_cons (purpose, value, chain)
  916.      tree purpose, value, chain;
  917. {
  918.   register tree node;
  919.   register struct obstack *ambient_obstack = current_obstack;
  920.   current_obstack = &temporary_obstack;
  921.  
  922.   node = make_node (TREE_LIST);
  923.   TREE_CHAIN (node) = chain;
  924.   TREE_PURPOSE (node) = purpose;
  925.   TREE_VALUE (node) = value;
  926.  
  927.   current_obstack = ambient_obstack;
  928.   return node;
  929. }
  930.  
  931. /* Same as `tree_cons', but save this node if the function's RTL is saved.  */
  932.  
  933. tree
  934. saveable_tree_cons (purpose, value, chain)
  935.      tree purpose, value, chain;
  936. {
  937.   register tree node;
  938.   register struct obstack *ambient_obstack = current_obstack;
  939.   current_obstack = saveable_obstack;
  940.  
  941.   node = make_node (TREE_LIST);
  942.   TREE_CHAIN (node) = chain;
  943.   TREE_PURPOSE (node) = purpose;
  944.   TREE_VALUE (node) = value;
  945.  
  946.   current_obstack = ambient_obstack;
  947.   return node;
  948. }
  949.  
  950. /* Return the last node in a chain of nodes (chained through TREE_CHAIN).  */
  951.  
  952. tree
  953. tree_last (chain)
  954.      register tree chain;
  955. {
  956.   register tree next;
  957.   if (chain)
  958.     while (next = TREE_CHAIN (chain))
  959.       chain = next;
  960.   return chain;
  961. }
  962.  
  963. /* Reverse the order of elements in the chain T,
  964.    and return the new head of the chain (old last element).  */
  965.  
  966. tree
  967. nreverse (t)
  968.      tree t;
  969. {
  970.   register tree prev = 0, decl, next;
  971.   for (decl = t; decl; decl = next)
  972.     {
  973.       next = TREE_CHAIN (decl);
  974.       TREE_CHAIN (decl) = prev;
  975.       prev = decl;
  976.     }
  977.   return prev;
  978. }
  979.  
  980. /* Return the size nominally occupied by an object of type TYPE
  981.    when it resides in memory.  The value is measured in units of bytes,
  982.    and its data type is that normally used for type sizes
  983.    (which is the first type created by make_signed_type or
  984.    make_unsigned_type).  */
  985.  
  986. tree
  987. size_in_bytes (type)
  988.      tree type;
  989. {
  990.   if (type == error_mark_node)
  991.     return integer_zero_node;
  992.   type = TYPE_MAIN_VARIANT (type);
  993.   if (TYPE_SIZE (type) == 0)
  994.     {
  995.       incomplete_type_error (0, type);
  996.       return integer_zero_node;
  997.     }
  998.   return convert_units (TYPE_SIZE (type), TYPE_SIZE_UNIT (type),
  999.             BITS_PER_UNIT);
  1000. }
  1001.  
  1002. /* Return the size of TYPE (in bytes) as an integer,
  1003.    or return -1 if the size can vary.  */
  1004.  
  1005. int
  1006. int_size_in_bytes (type)
  1007.      tree type;
  1008. {
  1009.   int size;
  1010.   if (type == error_mark_node)
  1011.     return 0;
  1012.   type = TYPE_MAIN_VARIANT (type);
  1013.   if (TYPE_SIZE (type) == 0)
  1014.     return -1;
  1015.   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
  1016.     return -1;
  1017.   size = TREE_INT_CST_LOW (TYPE_SIZE (type)) * TYPE_SIZE_UNIT (type);
  1018.   return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  1019. }
  1020.  
  1021. /* Return, as an INTEGER_CST node, the number of elements for
  1022.    TYPE (which is an ARRAY_TYPE).  */
  1023.  
  1024. tree
  1025. array_type_nelts (type)
  1026.      tree type;
  1027. {
  1028.   tree index_type = TYPE_DOMAIN (type);
  1029.   return (tree_int_cst_equal (TYPE_MIN_VALUE (index_type), integer_zero_node)
  1030.       ? TYPE_MAX_VALUE (index_type)
  1031.       : fold (build (MINUS_EXPR, integer_type_node,
  1032.              TYPE_MAX_VALUE (index_type),
  1033.              TYPE_MIN_VALUE (index_type))));
  1034. }
  1035.  
  1036. /* Return nonzero if arg is static -- a reference to an object in
  1037.    static storage.  This is not the same as the C meaning of `static'.  */
  1038.  
  1039. int
  1040. staticp (arg)
  1041.      tree arg;
  1042. {
  1043.   register enum tree_code code = TREE_CODE (arg);
  1044.  
  1045.   if ((code == VAR_DECL || code == FUNCTION_DECL || code == CONSTRUCTOR)
  1046.       && (TREE_STATIC (arg) || TREE_EXTERNAL (arg)))
  1047.     return 1;
  1048.  
  1049.   if (code == STRING_CST)
  1050.     return 1;
  1051.  
  1052.   if (code == COMPONENT_REF)
  1053.     return (DECL_VOFFSET (TREE_OPERAND (arg, 1)) == 0
  1054.         && staticp (TREE_OPERAND (arg, 0)));
  1055.  
  1056.   if (code == INDIRECT_REF)
  1057.     return TREE_LITERAL (TREE_OPERAND (arg, 0));
  1058.  
  1059.   if (code == ARRAY_REF)
  1060.     {
  1061.       if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
  1062.       && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
  1063.     return staticp (TREE_OPERAND (arg, 0));
  1064.     }
  1065.  
  1066.   return 0;
  1067. }
  1068.  
  1069. /* Return nonzero if REF is an lvalue valid for this language.
  1070.    Lvalues can be assigned, unless they have TREE_READONLY.
  1071.    Lvalues can have their address taken, unless they have TREE_REGDECL.  */
  1072.  
  1073. int
  1074. lvalue_p (ref)
  1075.      tree ref;
  1076. {
  1077.   register enum tree_code code = TREE_CODE (ref);
  1078.  
  1079.   if (language_lvalue_valid (ref))
  1080.     switch (code)
  1081.       {
  1082.       case COMPONENT_REF:
  1083.     return lvalue_p (TREE_OPERAND (ref, 0));
  1084.  
  1085.       case STRING_CST:
  1086.     return 1;
  1087.  
  1088.       case INDIRECT_REF:
  1089.       case ARRAY_REF:
  1090.       case VAR_DECL:
  1091.       case PARM_DECL:
  1092.       case RESULT_DECL:
  1093.       case ERROR_MARK:
  1094.     if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
  1095.         && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
  1096.       return 1;
  1097.     break;
  1098.  
  1099.       case NEW_EXPR:
  1100.     return 1;
  1101.  
  1102.       case CALL_EXPR:
  1103.     if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
  1104.       return 1;
  1105.       }
  1106.   return 0;
  1107. }
  1108.  
  1109. /* Return nonzero if REF is an lvalue valid for this language;
  1110.    otherwise, print an error message and return zero.  */
  1111.  
  1112. int
  1113. lvalue_or_else (ref, string)
  1114.      tree ref;
  1115.      char *string;
  1116. {
  1117.   int win = lvalue_p (ref);
  1118.   if (! win)
  1119.     error ("invalid lvalue in %s", string);
  1120.   return win;
  1121. }
  1122.  
  1123. /* This should be applied to any node which may be used in more than one place,
  1124.    but must be evaluated only once.  Normally, the code generator would
  1125.    reevaluate the node each time; this forces it to compute it once and save
  1126.    the result.  This is done by encapsulating the node in a SAVE_EXPR.  */
  1127.  
  1128. tree
  1129. save_expr (expr)
  1130.      tree expr;
  1131. {
  1132.   register tree t = fold (expr);
  1133.  
  1134.   /* If the tree evaluates to a constant, then we don't want to hide that
  1135.      fact (i.e. this allows further folding, and direct checks for constants).
  1136.      Since it is no problem to reevaluate literals, we just return the 
  1137.      literal node. */
  1138.  
  1139.   if (TREE_LITERAL (t) || TREE_READONLY (t) || TREE_CODE (t) == SAVE_EXPR)
  1140.     return t;
  1141.  
  1142.   return build (SAVE_EXPR, TREE_TYPE (expr), t, NULL);
  1143. }
  1144.  
  1145. /* Stabilize a reference so that we can use it any number of times
  1146.    without causing its operands to be evaluated more than once.
  1147.    Returns the stabilized reference.
  1148.  
  1149.    Also allows conversion expressions whose operands are references.
  1150.    Any other kind of expression is returned unchanged.  */
  1151.  
  1152. tree
  1153. stabilize_reference (ref)
  1154.      tree ref;
  1155. {
  1156.   register tree result;
  1157.   register enum tree_code code = TREE_CODE (ref);
  1158.  
  1159.   switch (code)
  1160.     {
  1161.     case VAR_DECL:
  1162.     case PARM_DECL:
  1163.     case RESULT_DECL:
  1164.       result = ref;
  1165.       break;
  1166.  
  1167.     case NOP_EXPR:
  1168.     case CONVERT_EXPR:
  1169.     case FLOAT_EXPR:
  1170.     case FIX_TRUNC_EXPR:
  1171.     case FIX_FLOOR_EXPR:
  1172.     case FIX_ROUND_EXPR:
  1173.     case FIX_CEIL_EXPR:
  1174.       result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
  1175.       break;
  1176.  
  1177.     case INDIRECT_REF:
  1178.       result = build_nt (INDIRECT_REF, save_expr (TREE_OPERAND (ref, 0)));
  1179.       break;
  1180.  
  1181.     case COMPONENT_REF:
  1182.       result = build_nt (COMPONENT_REF,
  1183.              stabilize_reference (TREE_OPERAND (ref, 0)),
  1184.              TREE_OPERAND (ref, 1));
  1185.       break;
  1186.  
  1187.     case ARRAY_REF:
  1188.       result = build_nt (ARRAY_REF, stabilize_reference (TREE_OPERAND (ref, 0)),
  1189.              save_expr (TREE_OPERAND (ref, 1)));
  1190.       break;
  1191.  
  1192.       /* If arg isn't a kind of lvalue we recognize, make no change.
  1193.      Caller should recognize the error for an invalid lvalue.  */
  1194.     default:
  1195.       return ref;
  1196.  
  1197.     case ERROR_MARK:
  1198.       return error_mark_node;
  1199.     }
  1200.  
  1201.   TREE_TYPE (result) = TREE_TYPE (ref);
  1202.   TREE_READONLY (result) = TREE_READONLY (ref);
  1203.   TREE_VOLATILE (result) = TREE_VOLATILE (ref);
  1204.   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
  1205.  
  1206.   return result;
  1207. }
  1208.  
  1209. /* Low-level constructors for expressions.  */
  1210.  
  1211. /* Build an expression of code CODE, data type TYPE,
  1212.    and operands as specified by the arguments ARG1 and following arguments.
  1213.    Expressions and reference nodes can be created this way.
  1214.    Constants, decls, types and misc nodes cannot be.  */
  1215.  
  1216. tree
  1217. #ifdef APPLE_HAX
  1218. build (code)
  1219.      enum tree_code code;
  1220. {
  1221.   register va_list p;
  1222. #else
  1223. build (va_alist)
  1224.      va_dcl
  1225. {
  1226.   register va_list p;
  1227.   enum tree_code code;
  1228. #endif /* APPLE_HAX */
  1229.   register tree t;
  1230.   register int length;
  1231.   register int i;
  1232.  
  1233. #ifdef APPLE_HAX
  1234.   va_start (p, code);
  1235. #else
  1236.   va_start (p);
  1237.  
  1238.   code = va_arg (p, enum tree_code);
  1239. #endif /* APPLE_HAX */
  1240.   t = make_node (code);
  1241.   length = tree_code_length[(int) code];
  1242.   TREE_TYPE (t) = va_arg (p, tree);
  1243.  
  1244.   if (length == 2)
  1245.     {
  1246.       /* This is equivalent to the loop below, but faster.  */
  1247.       register tree arg0 = va_arg (p, tree);
  1248.       register tree arg1 = va_arg (p, tree);
  1249.       TREE_OPERAND (t, 0) = arg0;
  1250.       TREE_OPERAND (t, 1) = arg1;
  1251.       TREE_VOLATILE (t)
  1252.     = (arg0 && TREE_VOLATILE (arg0)) || (arg1 && TREE_VOLATILE (arg1));
  1253.     }
  1254.   else
  1255.     {
  1256.       for (i = 0; i < length; i++)
  1257.     {
  1258.       register tree operand = va_arg (p, tree);
  1259.       TREE_OPERAND (t, i) = operand;
  1260.       if (operand && TREE_VOLATILE (operand))
  1261.         TREE_VOLATILE (t) = 1;
  1262.     }
  1263.     }
  1264.   va_end (p);
  1265.   return t;
  1266. }
  1267.  
  1268. /* Similar except don't specify the TREE_TYPE
  1269.    and leave the TREE_VOLATILE as 0.
  1270.    It is permissible for arguments to be null,
  1271.    or even garbage if their values do not matter.  */
  1272.  
  1273. tree
  1274. #ifdef APPLE_HAX
  1275. build_nt (code)
  1276.      enum tree_code code;
  1277. {
  1278.   register va_list p;
  1279. #else
  1280. build_nt (va_alist)
  1281.      va_dcl
  1282. {
  1283.   register va_list p;
  1284.   register enum tree_code code;
  1285. #endif /* APPLE_HAX */
  1286.   register tree t;
  1287.   register int length;
  1288.   register int i;
  1289.  
  1290. #ifdef APPLE_HAX
  1291.   va_start (p, code);
  1292. #else
  1293.   va_start (p);
  1294.  
  1295.   code = va_arg (p, enum tree_code);
  1296. #endif /* APPLE_HAX */
  1297.   t = make_node (code);
  1298.   length = tree_code_length[(int) code];
  1299.  
  1300.   for (i = 0; i < length; i++)
  1301.     TREE_OPERAND (t, i) = va_arg (p, tree);
  1302.  
  1303.   va_end (p);
  1304.   return t;
  1305. }
  1306.  
  1307. tree
  1308. build_op_identifier (op1, op2)
  1309.      tree op1, op2;
  1310. {
  1311.   register tree t = make_node (OP_IDENTIFIER);
  1312.   TREE_PURPOSE (t) = op1;
  1313.   TREE_VALUE (t) = op2;
  1314.   return t;
  1315. }
  1316.  
  1317. /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
  1318.    We do NOT enter this node in any sort of symbol table.
  1319.  
  1320.    layout_decl is used to set up the decl's storage layout.
  1321.    Other slots are initialized to 0 or null pointers.  */
  1322.  
  1323. tree
  1324. build_decl (code, name, type)
  1325.      enum tree_code code;
  1326.      tree name, type;
  1327. {
  1328.   register tree t;
  1329.  
  1330.   t = make_node (code);
  1331.  
  1332. /*  if (type == error_mark_node)
  1333.     type = integer_type_node; */
  1334. /* That is not done, deliberately, so that having error_mark_node
  1335.    as the type can suppress useless errors in the use of this variable.  */
  1336.  
  1337.   DECL_NAME (t) = name;
  1338.   if (name)
  1339.     {
  1340.       DECL_PRINT_NAME (t) = IDENTIFIER_POINTER (name);
  1341. #ifdef APPLE_HAX
  1342. #ifdef MARK_LABEL_SECTION
  1343.       DECL_ASSEMBLER_NAME (t) = (char *) add_section_mark (IDENTIFIER_POINTER (name));
  1344. #else
  1345.       DECL_ASSEMBLER_NAME (t) = IDENTIFIER_POINTER (name);
  1346. #endif
  1347. #else
  1348.       DECL_ASSEMBLER_NAME (t) = IDENTIFIER_POINTER (name);
  1349. #endif /* APPLE_HAX */
  1350.     }
  1351.   TREE_TYPE (t) = type;
  1352.   DECL_ARGUMENTS (t) = NULL_TREE;
  1353.   DECL_INITIAL (t) = NULL_TREE;
  1354.  
  1355.   if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
  1356.     layout_decl (t, 0);
  1357.   else if (code == FUNCTION_DECL)
  1358.     DECL_MODE (t) = FUNCTION_MODE;
  1359.  
  1360.   return t;
  1361. }
  1362.  
  1363. #if 0
  1364. /* Low-level constructors for statements.
  1365.    These constructors all expect source file name and line number
  1366.    as arguments, as well as enough arguments to fill in the data
  1367.    in the statement node.  */
  1368.  
  1369. tree
  1370. build_goto (filename, line, label)
  1371.      char *filename;
  1372.      int line;
  1373.      tree label;
  1374. {
  1375.   register tree t = make_node (GOTO_STMT);
  1376.   STMT_SOURCE_FILE (t) = filename;
  1377.   STMT_SOURCE_LINE (t) = line;
  1378.   STMT_BODY (t) = label;
  1379.   return t;
  1380. }
  1381.  
  1382. tree
  1383. build_return (filename, line, arg)
  1384.      char *filename;
  1385.      int line;
  1386.      tree arg;
  1387. {
  1388.   register tree t = make_node (RETURN_STMT);
  1389.  
  1390.   STMT_SOURCE_FILE (t) = filename;
  1391.   STMT_SOURCE_LINE (t) = line;
  1392.   STMT_BODY (t) = arg;
  1393.   return t;
  1394. }
  1395.  
  1396. tree
  1397. build_expr_stmt (filename, line, expr)
  1398.      char *filename;
  1399.      int line;
  1400.      tree expr;
  1401. {
  1402.   register tree t = make_node (EXPR_STMT);
  1403.  
  1404.   STMT_SOURCE_FILE (t) = filename;
  1405.   STMT_SOURCE_LINE (t) = line;
  1406.   STMT_BODY (t) = expr;
  1407.   return t;
  1408. }
  1409.  
  1410. tree
  1411. build_if (filename, line, cond, thenclause, elseclause)
  1412.      char *filename;
  1413.      int line;
  1414.      tree cond, thenclause, elseclause;
  1415. {
  1416.   register tree t = make_node (IF_STMT);
  1417.  
  1418.   STMT_SOURCE_FILE (t) = filename;
  1419.   STMT_SOURCE_LINE (t) = line;
  1420.   STMT_COND (t) = cond;
  1421.   STMT_THEN (t) = thenclause;
  1422.   STMT_ELSE (t) = elseclause;
  1423.   return t;
  1424. }
  1425.  
  1426. tree
  1427. build_exit (filename, line, cond)
  1428.      char *filename;
  1429.      int line;
  1430.      tree cond;
  1431. {
  1432.   register tree t = make_node (EXIT_STMT);
  1433.   STMT_SOURCE_FILE (t) = filename;
  1434.   STMT_SOURCE_LINE (t) = line;
  1435.   STMT_BODY (t) = cond;
  1436.   return t;
  1437. }
  1438.  
  1439. tree
  1440. build_asm_stmt (filename, line, asmcode)
  1441.      char *filename;
  1442.      int line;
  1443.      tree asmcode;
  1444. {
  1445.   register tree t = make_node (ASM_STMT);
  1446.   STMT_SOURCE_FILE (t) = filename;
  1447.   STMT_SOURCE_LINE (t) = line;
  1448.   STMT_BODY (t) = asmcode;
  1449.   return t;
  1450. }
  1451.  
  1452. tree
  1453. build_case (filename, line, object, cases)
  1454.      char *filename;
  1455.      int line;
  1456.      tree object, cases;
  1457. {
  1458.   register tree t = make_node (CASE_STMT);
  1459.   STMT_SOURCE_FILE (t) = filename;
  1460.   STMT_SOURCE_LINE (t) = line;
  1461.   STMT_CASE_INDEX (t) = object;
  1462.   STMT_CASE_LIST (t) = cases;
  1463.   return t;
  1464. }
  1465.  
  1466. tree
  1467. build_loop (filename, line, body)
  1468.      char *filename;
  1469.      int line;
  1470.      tree body;
  1471. {
  1472.   register tree t = make_node (LOOP_STMT);
  1473.   STMT_SOURCE_FILE (t) = filename;
  1474.   STMT_SOURCE_LINE (t) = line;
  1475.   STMT_BODY (t) = body;
  1476.   return t;
  1477. }
  1478.  
  1479. tree
  1480. build_compound (filename, line, body)
  1481.      char *filename;
  1482.      int line;
  1483.      tree body;
  1484. {
  1485.   register tree t = make_node (COMPOUND_STMT);
  1486.   STMT_SOURCE_FILE (t) = filename;
  1487.   STMT_SOURCE_LINE (t) = line;
  1488.   STMT_BODY (t) = body;
  1489.   return t;
  1490. }
  1491.  
  1492. #endif /* 0 */
  1493.  
  1494. /* LET_STMT nodes are used to represent the structure of binding contours
  1495.    and declarations, once those contours have been exited and their contents
  1496.    compiled.  This information is used for outputting debugging info.  */
  1497.  
  1498. tree
  1499. build_let (filename, line, vars, subblocks, supercontext, tags)
  1500.      char *filename;
  1501.      int line;
  1502.      tree vars, subblocks, supercontext, tags;
  1503. {
  1504.   register tree t = make_node (LET_STMT);
  1505.   STMT_SOURCE_FILE (t) = filename;
  1506.   STMT_SOURCE_LINE (t) = line;
  1507.   STMT_VARS (t) = vars;
  1508.   STMT_SUBBLOCKS (t) = subblocks;
  1509.   STMT_SUPERCONTEXT (t) = supercontext;
  1510.   STMT_BIND_SIZE (t) = 0;
  1511.   STMT_TYPE_TAGS (t) = tags;
  1512.   return t;
  1513. }
  1514.  
  1515. /* Return a type like TYPE except that its TREE_READONLY is CONSTP
  1516.    and its TREE_VOLATILE is VOLATILEP.
  1517.  
  1518.    Such variant types already made are recorded so that duplicates
  1519.    are not made.
  1520.  
  1521.    A variant types should never be used as the type of an expression.
  1522.    Always copy the variant information into the TREE_READONLY
  1523.    and TREE_VOLATILE of the expression, and then give the expression
  1524.    as its type the "main variant", the variant whose TREE_READONLY
  1525.    and TREE_VOLATILE are zero.  Use TYPE_MAIN_VARIANT to find the
  1526.    main variant.  */
  1527.  
  1528. tree
  1529. build_type_variant (type, constp, volatilep, pascalp)
  1530.      tree type;
  1531.      int constp, volatilep, pascalp;
  1532. {
  1533.   register tree t, m = TYPE_MAIN_VARIANT (type);
  1534.   register struct obstack *ambient_obstack = current_obstack;
  1535.  
  1536.   /* Treat any nonzero argument as 1.  */
  1537.   constp = !!constp;
  1538.   volatilep = !!volatilep;
  1539.   pascalp = !!pascalp;
  1540.  
  1541.   /* First search the chain variants for one that is what we want.  */
  1542.  
  1543.   for (t = m; t; t = TYPE_NEXT_VARIANT (t))
  1544.     if (constp == TREE_READONLY (t)
  1545.     && volatilep == TREE_VOLATILE (t) && pascalp == TREE_PASCAL (t))
  1546.       return t;
  1547.  
  1548.   /* We need a new one.  */
  1549.   current_obstack
  1550.     = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
  1551.  
  1552.   t = copy_node (type);
  1553.   TREE_READONLY (t) = constp;
  1554.   TREE_VOLATILE (t) = volatilep;
  1555.   TREE_PASCAL (t) = pascalp;
  1556.   TYPE_POINTER_TO (t) = 0;
  1557.   TYPE_REFERENCE_TO (t) = 0;
  1558.  
  1559.   /* Add this type to the chain of variants of TYPE.  */
  1560.   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
  1561.   TYPE_NEXT_VARIANT (m) = t;
  1562.  
  1563.   current_obstack = ambient_obstack;
  1564.   return t;
  1565. }
  1566.  
  1567. /* Hashing of types so that we don't make duplicates.
  1568.    The entry point is `type_hash_canon'.  */
  1569.  
  1570. /* Each hash table slot is a bucket containing a chain
  1571.    of these structures.  */
  1572.  
  1573. struct type_hash
  1574. {
  1575.   struct type_hash *next;    /* Next structure in the bucket.  */
  1576.   int hashcode;            /* Hash code of this type.  */
  1577.   tree type;            /* The type recorded here.  */
  1578. };
  1579.  
  1580. /* Now here is the hash table.  When recording a type, it is added
  1581.    to the slot whose index is the hash code mod the table size.
  1582.    Note that the hash table is used for several kinds of types
  1583.    (function types, array types and array index range types, for now).
  1584.    While all these live in the same table, they are completely independent,
  1585.    and the hash code is computed differently for each of these.  */
  1586.  
  1587. #define TYPE_HASH_SIZE 59
  1588. struct type_hash *type_hash_table[TYPE_HASH_SIZE];
  1589.  
  1590. /* Here is how primitive or already-canonicalized types' hash
  1591.    codes are made.  */
  1592. #define TYPE_HASH(TYPE) TREE_UID (TYPE)
  1593.  
  1594. /* Compute a hash code for a list of types (chain of TREE_LIST nodes
  1595.    with types in the TREE_VALUE slots), by adding the hash codes
  1596.    of the individual types.  */
  1597.  
  1598. int
  1599. type_hash_list (list)
  1600.      tree list;
  1601. {
  1602.   register int hashcode;
  1603.   register tree tail;
  1604.   for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
  1605.     hashcode += TYPE_HASH (TREE_VALUE (tail));
  1606.   return hashcode;
  1607. }
  1608.  
  1609. /* Look in the type hash table for a type isomorphic to TYPE.
  1610.    If one is found, return it.  Otherwise return 0.  */
  1611.  
  1612. tree
  1613. type_hash_lookup (hashcode, type)
  1614.      int hashcode;
  1615.      tree type;
  1616. {
  1617.   register struct type_hash *h;
  1618.   for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
  1619.     if (h->hashcode == hashcode
  1620.     && TREE_CODE (h->type) == TREE_CODE (type)
  1621.     && TREE_TYPE (h->type) == TREE_TYPE (type)
  1622.     && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
  1623.         || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
  1624.                    TYPE_MAX_VALUE (type)))
  1625.     && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
  1626.         || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
  1627.                    TYPE_MIN_VALUE (type)))
  1628.     && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
  1629.         || (TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
  1630.         && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
  1631.         && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
  1632.       return h->type;
  1633.   return 0;
  1634. }
  1635.  
  1636. /* Add an entry to the type-hash-table
  1637.    for a type TYPE whose hash code is HASHCODE.  */
  1638.  
  1639. void
  1640. type_hash_add (hashcode, type)
  1641.      int hashcode;
  1642.      tree type;
  1643. {
  1644.   register struct type_hash *h;
  1645.  
  1646.   h = (struct type_hash *) oballoc (sizeof (struct type_hash));
  1647.   h->hashcode = hashcode;
  1648.   h->type = type;
  1649.   h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
  1650.   type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
  1651. }
  1652.  
  1653. /* Given TYPE, and HASHCODE its hash code, return the canonical
  1654.    object for an identical type if one already exists.
  1655.    Otherwise, return TYPE, and record it as the canonical object
  1656.    if it is a permanent object.
  1657.  
  1658.    To use this function, first create a type of the sort you want.
  1659.    Then compute its hash code from the fields of the type that
  1660.    make it different from other similar types.
  1661.    Then call this function and use the value.
  1662.    This function frees the type you pass in if it is a duplicate.  */
  1663.  
  1664. /* Set to 1 to debug without canonicalization.  Never set by program.  */
  1665. #ifdef APPLE_HAX
  1666. int debug_no_type_hash =
  1667. #ifdef __GNUC__
  1668. 1
  1669. #else
  1670. 0
  1671. #endif
  1672. ;
  1673. #else
  1674. int debug_no_type_hash = 0;
  1675. #endif /* APPLE_HAX */
  1676.  
  1677. tree
  1678. type_hash_canon (hashcode, type)
  1679.      int hashcode;
  1680.      tree type;
  1681. {
  1682.   tree t1;
  1683.  
  1684.   if (debug_no_type_hash)
  1685.     return type;
  1686.  
  1687.   t1 = type_hash_lookup (hashcode, type);
  1688.   if (t1 != 0)
  1689.     {
  1690.       struct obstack *o
  1691.     = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
  1692.       obstack_free (o, type);
  1693.       return t1;
  1694.     }
  1695.  
  1696.   /* If this is a new type, record it for later reuse.  */
  1697.   if (current_obstack == &permanent_obstack)
  1698.     type_hash_add (hashcode, type);
  1699.  
  1700.   return type;
  1701. }
  1702.  
  1703. /* Given two lists of types
  1704.    (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
  1705.    return 1 if the lists contain the same types in the same order.
  1706.    Also, the TREE_PURPOSEs must match.  */
  1707.  
  1708. int
  1709. type_list_equal (l1, l2)
  1710.      tree l1, l2;
  1711. {
  1712.   register tree t1, t2;
  1713.   for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
  1714.     {
  1715.       if (TREE_VALUE (t1) != TREE_VALUE (t2))
  1716.     return 0;
  1717.       if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
  1718.       && !simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
  1719.     return 0;
  1720.     }
  1721.  
  1722.   return t1 == t2;
  1723. }
  1724.  
  1725. /* Nonzero if integer constants T1 and T2
  1726.    represent the same constant value.  */
  1727.  
  1728. int
  1729. tree_int_cst_equal (t1, t2)
  1730.      tree t1, t2;
  1731. {
  1732.   if (t1 == t2)
  1733.     return 1;
  1734.   if (t1 == 0 || t2 == 0)
  1735.     return 0;
  1736.   if (TREE_CODE (t1) == INTEGER_CST
  1737.       && TREE_CODE (t2) == INTEGER_CST
  1738.       && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
  1739.       && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
  1740.     return 1;
  1741.   return 0;
  1742. }
  1743.  
  1744. /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
  1745.    The precise way of comparison depends on their data type.  */
  1746.  
  1747. int
  1748. tree_int_cst_lt (t1, t2)
  1749.      tree t1, t2;
  1750. {
  1751.   if (t1 == t2)
  1752.     return 0;
  1753.  
  1754.   if (!TREE_UNSIGNED (TREE_TYPE (t1)))
  1755.     return INT_CST_LT (t1, t2);
  1756.   return INT_CST_LT_UNSIGNED (t1, t2);
  1757. }
  1758.  
  1759. /* Compare two constructor-element-type constants.  */
  1760.  
  1761. int
  1762. simple_cst_equal (t1, t2)
  1763.      tree t1, t2;
  1764. {
  1765.   register enum tree_code code1, code2;
  1766.  
  1767.   if (t1 == t2)
  1768.     return 1;
  1769.   if (t1 == 0 || t2 == 0)
  1770.     return 0;
  1771.  
  1772.   code1 = TREE_CODE (t1);
  1773.   code2 = TREE_CODE (t2);
  1774.  
  1775.   if (code1 == NOP_EXPR || code1 == CONVERT_EXPR)
  1776.     if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
  1777.       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
  1778.     else
  1779.       return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
  1780.   else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
  1781.     return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
  1782.  
  1783.   if (code1 != code2)
  1784.     return 0;
  1785.  
  1786.   switch (code1)
  1787.     {
  1788.     case INTEGER_CST:
  1789.       return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
  1790.     && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
  1791.  
  1792.     case REAL_CST:
  1793.       return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
  1794.  
  1795.     case STRING_CST:
  1796.       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
  1797.     && !strcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2));
  1798.  
  1799.     case CONSTRUCTOR:
  1800.       abort ();
  1801.  
  1802.     case VAR_DECL:
  1803.     case PARM_DECL:
  1804.     case CONST_DECL:
  1805.       return 0;
  1806.  
  1807.     case PLUS_EXPR:
  1808.     case MINUS_EXPR:
  1809.     case MULT_EXPR:
  1810.     case TRUNC_DIV_EXPR:
  1811.     case TRUNC_MOD_EXPR:
  1812.     case LSHIFT_EXPR:
  1813.     case RSHIFT_EXPR:
  1814.       return (simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
  1815.           && simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
  1816.  
  1817.     case NEGATE_EXPR:
  1818.     case ADDR_EXPR:
  1819.     case REFERENCE_EXPR:
  1820.     case INDIRECT_REF:
  1821.       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
  1822.  
  1823.     default:
  1824.       abort ();
  1825.     }
  1826. }
  1827.  
  1828. /* Constructors for pointer, array and function types.
  1829.    (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
  1830.    constructed by language-dependent code, not here.)  */
  1831.  
  1832. /* Construct, lay out and return the type of pointers to TO_TYPE.
  1833.    If such a type has already been constructed, reuse it.  */
  1834.  
  1835. tree
  1836. build_pointer_type (to_type)
  1837.      tree to_type;
  1838. {
  1839.   register tree t = TYPE_POINTER_TO (to_type);
  1840.   register struct obstack *ambient_obstack = current_obstack;
  1841.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  1842.  
  1843.   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
  1844.  
  1845.   if (t)
  1846.     return t;
  1847.  
  1848.   /* We need a new one.  If TO_TYPE is permanent, make this permanent too.  */
  1849.   if (TREE_PERMANENT (to_type))
  1850.     {
  1851.       current_obstack = &permanent_obstack;
  1852.       saveable_obstack = &permanent_obstack;
  1853.     }
  1854.  
  1855.   t = make_node (POINTER_TYPE);
  1856.   TREE_TYPE (t) = to_type;
  1857.  
  1858.   /* Record this type as the pointer to TO_TYPE.  */
  1859.   TYPE_POINTER_TO (to_type) = t;
  1860.  
  1861.   /* Lay out the type.  This function has many callers that are concerned
  1862.      with expression-construction, and this simplifies them all.
  1863.      Also, it guarantees the TYPE_SIZE is permanent if the type is.  */
  1864.   layout_type (t);
  1865.  
  1866.   current_obstack = ambient_obstack;
  1867.   saveable_obstack = ambient_saveable_obstack;
  1868.   return t;
  1869. }
  1870.  
  1871. /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
  1872.    MAXVAL should be the maximum value in the domain
  1873.    (one less than the length of the array).  */
  1874.  
  1875. tree
  1876. build_index_type (maxval)
  1877.      tree maxval;
  1878. {
  1879.   register tree itype = make_node (INTEGER_TYPE);
  1880.   int maxint = TREE_INT_CST_LOW (maxval);
  1881.   TYPE_PRECISION (itype) = BITS_PER_WORD;
  1882.   TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
  1883.   TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
  1884.   TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
  1885.   TYPE_MODE (itype) = SImode;
  1886.   TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
  1887.   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
  1888.   TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
  1889.   return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
  1890. }
  1891.  
  1892. /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
  1893.    and number of elements specified by the range of values of INDEX_TYPE.
  1894.    If such a type has already been constructed, reuse it.  */
  1895.  
  1896. tree
  1897. build_array_type (elt_type, index_type)
  1898.      tree elt_type, index_type;
  1899. {
  1900.   register tree t = make_node (ARRAY_TYPE);
  1901.   int hashcode;
  1902.  
  1903.   if (TREE_CODE (elt_type) == FUNCTION_TYPE)
  1904.     {
  1905.       error ("arrays of functions are not meaningful");
  1906.       elt_type = integer_type_node;
  1907.     }
  1908.  
  1909.   TREE_TYPE (t) = elt_type;
  1910.   TYPE_DOMAIN (t) = index_type;
  1911.  
  1912.   /* Make sure TYPE_POINTER_TO (elt_type) is filled in.  */
  1913.   build_pointer_type (elt_type);
  1914.  
  1915.   if (index_type == 0)
  1916.     return t;
  1917.  
  1918.   hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
  1919.   t = type_hash_canon (hashcode, t);
  1920.  
  1921.   if (TYPE_SIZE (t) == 0)
  1922.     layout_type (t);
  1923.   return t;
  1924. }
  1925.  
  1926. /* Construct, lay out and return
  1927.    the type of functions returning type VALUE_TYPE
  1928.    given arguments of types ARG_TYPES.
  1929.    ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
  1930.    are data type nodes for the arguments of the function.
  1931.    If such a type has already been constructed, reuse it.  */
  1932.  
  1933. tree
  1934. build_function_type (value_type, arg_types)
  1935.      tree value_type, arg_types;
  1936. {
  1937.   register tree t;
  1938.   int hashcode;
  1939. #if 0 /* def APPLE_C */
  1940.   extern int is_pascal_function_type;
  1941. #endif
  1942.  
  1943.   if (TREE_CODE (value_type) == FUNCTION_TYPE
  1944.       || TREE_CODE (value_type) == ARRAY_TYPE)
  1945.     {
  1946.       error ("function return type cannot be function or array");
  1947.       value_type = integer_type_node;
  1948.     }
  1949.  
  1950.   /* Make a node of the sort we want.  */
  1951.   t = make_node (FUNCTION_TYPE);
  1952.   TREE_TYPE (t) = value_type;
  1953.   TYPE_ARG_TYPES (t) = arg_types;
  1954.  
  1955.   /* If we already have such a type, use the old one and free this one.  */
  1956.   hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
  1957.   t = type_hash_canon (hashcode, t);
  1958.  
  1959.   if (TYPE_SIZE (t) == 0)
  1960.     layout_type (t);
  1961.   return t;
  1962. }
  1963.  
  1964. /* Build the node for the type of references-to-TO_TYPE.  */
  1965.  
  1966. tree
  1967. build_reference_type (to_type)
  1968.      tree to_type;
  1969. {
  1970.   register tree t = TYPE_REFERENCE_TO (to_type);
  1971.   register struct obstack *ambient_obstack = current_obstack;
  1972.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  1973.  
  1974.   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
  1975.  
  1976.   if (t)
  1977.     return t;
  1978.  
  1979.   /* We need a new one.  If TO_TYPE is permanent, make this permanent too.  */
  1980.   if (TREE_PERMANENT (to_type))
  1981.     {
  1982.       current_obstack = &permanent_obstack;
  1983.       saveable_obstack = &permanent_obstack;
  1984.     }
  1985.  
  1986.   t = make_node (REFERENCE_TYPE);
  1987.   TREE_TYPE (t) = to_type;
  1988.  
  1989.   /* Record this type as the pointer to TO_TYPE.  */
  1990.   TYPE_REFERENCE_TO (to_type) = t;
  1991.  
  1992.   layout_type (t);
  1993.  
  1994.   current_obstack = ambient_obstack;
  1995.   saveable_obstack = ambient_saveable_obstack;
  1996.   return t;
  1997. }
  1998.  
  1999. /* Construct, lay out and return the type of methods belonging to class
  2000.    BASETYPE and whose arguments and values are described by TYPE.
  2001.    If that type exists already, reuse it.
  2002.    TYPE must be a FUNCTION_TYPE node.  */
  2003.  
  2004. tree
  2005. build_method_type (basetype, type)
  2006.      tree basetype, type;
  2007. {
  2008.   register tree t;
  2009.   int hashcode;
  2010.  
  2011.   /* Make a node of the sort we want.  */
  2012.   t = make_node (METHOD_TYPE);
  2013.  
  2014.   if (TREE_CODE (type) != FUNCTION_TYPE)
  2015.     abort ();
  2016.  
  2017.   TYPE_METHOD_BASETYPE (t) = basetype;
  2018.   TREE_TYPE (t) = TREE_TYPE (type);
  2019.  
  2020.   /* The actual arglist for this function includes a "hidden" argument
  2021.      which is "this".  Put it into the list of argument types.  */
  2022.  
  2023.   TYPE_ARG_TYPES (t)
  2024.     = tree_cons (NULL, build_pointer_type (basetype), TYPE_ARG_TYPES (type));
  2025.  
  2026.   /* If we already have such a type, use the old one and free this one.  */
  2027.   hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
  2028.   t = type_hash_canon (hashcode, t);
  2029.  
  2030.   if (TYPE_SIZE (t) == 0)
  2031.     layout_type (t);
  2032.  
  2033.   return t;
  2034. }
  2035.  
  2036. /* Construct, lay out and return the type of methods belonging to class
  2037.    BASETYPE and whose arguments and values are described by TYPE.
  2038.    If that type exists already, reuse it.
  2039.    TYPE must be a FUNCTION_TYPE node.  */
  2040.  
  2041. tree
  2042. build_offset_type (basetype, type)
  2043.      tree basetype, type;
  2044. {
  2045.   register tree t;
  2046.   int hashcode;
  2047.  
  2048.   /* Make a node of the sort we want.  */
  2049.   t = make_node (OFFSET_TYPE);
  2050.  
  2051.   TYPE_OFFSET_BASETYPE (t) = basetype;
  2052.   TREE_TYPE (t) = type;
  2053.  
  2054.   /* If we already have such a type, use the old one and free this one.  */
  2055.   hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
  2056.   t = type_hash_canon (hashcode, t);
  2057.  
  2058.   if (TYPE_SIZE (t) == 0)
  2059.     layout_type (t);
  2060.  
  2061.   return t;
  2062. }
  2063.  
  2064. /* Return OP, stripped of any conversions to wider types as much as is safe.
  2065.    Converting the value back to OP's type makes a value equivalent to OP.
  2066.  
  2067.    If FOR_TYPE is nonzero, we return a value which, if converted to
  2068.    type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
  2069.  
  2070.    If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
  2071.    narrowest type that can hold the value, even if they don't exactly fit.
  2072.    Otherwise, bit-field references are changed to a narrower type
  2073.    only if they can be fetched directly from memory in that type.
  2074.  
  2075.    OP must have integer, real or enumeral type.  Pointers are not allowed!
  2076.  
  2077.    There are some cases where the obvious value we could return
  2078.    would regenerate to OP if converted to OP's type, 
  2079.    but would not extend like OP to wider types.
  2080.    If FOR_TYPE indicates such extension is contemplated, we eschew such values.
  2081.    For example, if OP is (unsigned short)(signed char)-1,
  2082.    we avoid returning (signed char)-1 if FOR_TYPE is int,
  2083.    even though extending that to an unsigned short would regenerate OP,
  2084.    since the result of extending (signed char)-1 to (int)
  2085.    is different from (int) OP.  */
  2086.  
  2087. tree
  2088. get_unwidened (op, for_type)
  2089.      register tree op;
  2090.      tree for_type;
  2091. {
  2092.   /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension.  */
  2093.   /* TYPE_PRECISION is safe in place of type_precision since
  2094.      pointer types are not allowed.  */
  2095.   register tree type = TREE_TYPE (op);
  2096.   register int final_prec = TYPE_PRECISION (for_type != 0 ? for_type : type);
  2097.   register int uns
  2098.     = (for_type != 0 && for_type != type
  2099.        && final_prec > TYPE_PRECISION (type)
  2100.        && TREE_UNSIGNED (type));
  2101.   register tree win = op;
  2102.  
  2103.   while (TREE_CODE (op) == NOP_EXPR)
  2104.     {
  2105.       register int bitschange
  2106.     = TYPE_PRECISION (TREE_TYPE (op))
  2107.       - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
  2108.  
  2109.       /* Truncations are many-one so cannot be removed.
  2110.      Unless we are later going to truncate down even farther.  */
  2111.       if (bitschange < 0
  2112.       && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
  2113.     break;
  2114.  
  2115.       /* See what's inside this conversion.  If we decide to strip it,
  2116.      we will set WIN.  */
  2117.       op = TREE_OPERAND (op, 0);
  2118.  
  2119.       /* If we have not stripped any zero-extensions (uns is 0),
  2120.      we can strip any kind of extension.
  2121.      If we have previously stripped a zero-extension,
  2122.      only zero-extensions can safely be stripped.
  2123.      Any extension can be stripped if the bits it would produce
  2124.      are all going to be discarded later by truncating to FOR_TYPE.  */
  2125.  
  2126.       if (bitschange > 0)
  2127.     {
  2128.       if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
  2129.         win = op;
  2130.       /* TREE_UNSIGNED says whether this is a zero-extension.
  2131.          Let's avoid computing it if it does not affect WIN
  2132.          and if UNS will not be needed again.  */
  2133.       if ((uns || TREE_CODE (op) == NOP_EXPR)
  2134.           && TREE_UNSIGNED (TREE_TYPE (op)))
  2135.         {
  2136.           uns = 1;
  2137.           win = op;
  2138.         }
  2139.     }
  2140.     }
  2141.  
  2142.   if (TREE_CODE (op) == COMPONENT_REF
  2143.       /* Since type_for_size always gives an integer type.  */
  2144.       && TREE_CODE (type) != REAL_TYPE)
  2145.     {
  2146.       int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
  2147.                * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
  2148.       type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
  2149.  
  2150.       /* We can get this structure field in the narrowest type it fits in.
  2151.      If FOR_TYPE is 0, do this only for a field that matches the
  2152.      narrower type exactly and is aligned for it (i.e. mode isn't BI).
  2153.      The resulting extension to its nominal type (a fullword type)
  2154.      must fit the same conditions as for other extensions.  */
  2155.  
  2156.       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
  2157.       && (for_type || DECL_MODE (TREE_OPERAND (op, 1)) != BImode)
  2158.       && (! uns || final_prec <= innerprec
  2159.           || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
  2160.       && type != 0)
  2161.     {
  2162.       win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
  2163.                TREE_OPERAND (op, 1));
  2164.       TREE_VOLATILE (win) = TREE_VOLATILE (op);
  2165.       TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
  2166.     }
  2167.     }
  2168.   return win;
  2169. }
  2170.  
  2171. /* Return OP or a simpler expression for a narrower value
  2172.    which can be sign-extended or zero-extended to give back OP.
  2173.    Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
  2174.    or 0 if the value should be sign-extended.  */
  2175.  
  2176. tree
  2177. get_narrower (op, unsignedp_ptr)
  2178.      register tree op;
  2179.      int *unsignedp_ptr;
  2180. {
  2181.   register int uns = 0;
  2182.   int first = 1;
  2183.   register tree win = op;
  2184.  
  2185.   while (TREE_CODE (op) == NOP_EXPR)
  2186.     {
  2187.       register int bitschange
  2188.     = TYPE_PRECISION (TREE_TYPE (op))
  2189.       - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
  2190.  
  2191.       /* Truncations are many-one so cannot be removed.  */
  2192.       if (bitschange < 0)
  2193.     break;
  2194.  
  2195.       /* See what's inside this conversion.  If we decide to strip it,
  2196.      we will set WIN.  */
  2197.       op = TREE_OPERAND (op, 0);
  2198.  
  2199.       if (bitschange > 0)
  2200.     {
  2201.       /* An extension: the outermost one can be stripped,
  2202.          but remember whether it is zero or sign extension.  */
  2203.       if (first)
  2204.         uns = TREE_UNSIGNED (TREE_TYPE (op));
  2205.       /* Otherwise, if a sign extension has been stripped,
  2206.          only sign extensions can now be stripped;
  2207.          if a zero extension has been stripped, only zero-extensions.  */
  2208.       else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
  2209.         break;
  2210.       first = 0;
  2211.     }
  2212.       /* A change in nominal type can always be stripped.  */
  2213.  
  2214.       win = op;
  2215.     }
  2216.  
  2217.   if (TREE_CODE (op) == COMPONENT_REF
  2218.       /* Since type_for_size always gives an integer type.  */
  2219.       && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
  2220.     {
  2221.       int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
  2222.                * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
  2223.       tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
  2224.  
  2225.       /* We can get this structure field in a narrower type that fits it,
  2226.      but the resulting extension to its nominal type (a fullword type)
  2227.      must satisfy the same conditions as for other extensions.
  2228.  
  2229.      Do this only for fields that are aligned (not BImode),
  2230.      because when bit-field insns will be used there is no
  2231.      advantage in doing this.  */
  2232.  
  2233.       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
  2234.       && DECL_MODE (TREE_OPERAND (op, 1)) != BImode
  2235.       && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
  2236.       && type != 0)
  2237.     {
  2238.       if (first)
  2239.         uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
  2240.       win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
  2241.                TREE_OPERAND (op, 1));
  2242.       TREE_VOLATILE (win) = TREE_VOLATILE (op);
  2243.       TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
  2244.     }
  2245.     }
  2246.   *unsignedp_ptr = uns;
  2247.   return win;
  2248. }
  2249.  
  2250. /* Return the precision of a type, for arithmetic purposes.
  2251.    Supports all types on which arithmetic is possible
  2252.    (including pointer types).
  2253.    It's not clear yet what will be right for complex types.  */
  2254.  
  2255. int
  2256. type_precision (type)
  2257.      register tree type;
  2258. {
  2259.   return ((TREE_CODE (type) == INTEGER_TYPE
  2260.        || TREE_CODE (type) == ENUMERAL_TYPE
  2261.        || TREE_CODE (type) == REAL_TYPE)
  2262.       ? TYPE_PRECISION (type) : POINTER_SIZE);
  2263. }
  2264.  
  2265. /* Nonzero if integer constant C has a value that is permissible
  2266.    for type TYPE (an INTEGER_TYPE).  */
  2267.  
  2268. int
  2269. int_fits_type_p (c, type)
  2270.      tree c, type;
  2271. {
  2272.   if (TREE_UNSIGNED (type))
  2273.     return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
  2274.         && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type)));
  2275.   else
  2276.     return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
  2277.         && !INT_CST_LT (c, TYPE_MIN_VALUE (type)));
  2278. }
  2279.